package lp.xian.materials.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import lp.xian.materials.vo.InventoryVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lp.xian.materials.mapper.InventoryMapper;
import lp.xian.materials.domain.Inventory;
import lp.xian.materials.service.IInventoryService;

/**
 * 库存列表Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-04
 */
@Service
public class InventoryServiceImpl implements IInventoryService
{
    @Autowired
    private InventoryMapper inventoryMapper;
//    @Autowired
//    private ProductInventoryMapper productInventoryMapper;


    /**
     * 查询库存列表
     *
     * @param inventoryId 库存列表主键
     * @return 库存列表
     */
    @Override
    public Inventory selectInventoryByInventoryId(Integer inventoryId)
    {
        return inventoryMapper.selectInventoryByInventoryId(inventoryId);
    }

    /**
     * 查询库存列表列表
     *
     * @param inventory 库存列表
     * @return 库存列表
     */
    @Override
    public List<Inventory> selectInventoryList(Inventory inventory)
    {
        return inventoryMapper.selectInventoryList(inventory);
    }

    /**
     * 新增库存列表
     *
     * @param inventory 库存列表
     * @return 结果
     */
    @Override
    public int insertInventory(Inventory inventory)
    {
        inventory.setCreateTime(DateUtils.getNowDate());
        return inventoryMapper.insertInventory(inventory);
    }

    /**
     * 修改库存列表
     *
     * @param inventory 库存列表
     * @return 结果
     */
    @Override
    public int updateInventory(Inventory inventory)
    {
        inventory.setUpdateTime(DateUtils.getNowDate());
        return inventoryMapper.updateInventory(inventory);
    }

    /**
     * 批量删除库存列表
     *
     * @param inventoryIds 需要删除的库存列表主键
     * @return 结果
     */
    @Override
    public int deleteInventoryByInventoryIds(Integer[] inventoryIds)
    {
        return inventoryMapper.deleteInventoryByInventoryIds(inventoryIds);
    }

    /**
     * 删除库存列表信息
     *
     * @param inventoryId 库存列表主键
     * @return 结果
     */
    @Override
    public int deleteInventoryByInventoryId(Integer inventoryId)
    {
        return inventoryMapper.deleteInventoryByInventoryId(inventoryId);
    }

    @Override
    public int pullInventory(InventoryVo inventoryVo) {
     /*   //(1)申领的时候需要将原材料添加到生产耗材
        ProductInventory productionInventory=new ProductInventory();
        productionInventory.setProductionInventoryName(inventoryVo.getInventoryName());
        productionInventory.setProductionInventoryNum(inventoryVo.getInventoryNum());
        productionInventory.setProductionInventoryPrice(inventoryVo.getInventoryPrice());
        productionInventory.setProductionInventoryUnit(inventoryVo.getInventoryUnit());
        productionInventory.setAuditStatus(0);
        productionInventory.setCreateBy("系统生成");
        productionInventory.setCreateTime(new Date());
        int num=productInventoryMapper.insertProductInventory(productionInventory);
        if (num==1){
            //需要查询最大的数据
            ProductInventory p1=productInventoryMapper.maxInventory();
            //(2)更新库存数据
            //Integer nums=p1.getProductionInventoryNum();//申领数据
            //数量减少
            Inventory inv=inventoryMapper.selectInventoryByInventoryId(inventoryVo.getInventoryId());
            Integer InvNum=inv.getInventoryNum()-p1.getProductionInventoryNum();
            Inventory inventory=new Inventory();
            inventory.setInventoryId(inventoryVo.getInventoryId());
            inventory.setInventoryNum(InvNum);
            inventory.setUpdateTime(new Date());
            inventory.setUpdateBy(SecurityUtils.getUsername());
            return inventoryMapper.updateInventory(inventory);
        }*/

        return 0;
    }
}
